package com.yotoodesign.dianba.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.yotoodesign.dianba.bean.Singer;
import com.yotoodesign.dianba.bean.Song;
import com.yotoodesign.dianba.bean.Tag;

/*
 * Top10歌手服务
 */
public class Top10SingerService extends Pager {
	private static Top10SingerService singerService;
	
	public static Top10SingerService getInstance(){
		if(singerService==null){
			singerService = new Top10SingerService();
		}
		return singerService;
	}
	
	
	public List<Singer> prepare(String xmlFile){
		File file = new File(xmlFile);
		//如果当前文件不存在，取当前应用内的文件
		boolean exists = file.exists();
		if(!exists){
			String[] ps = xmlFile.split("/");
			if(ps.length>0){
				xmlFile = "/"+ps[ps.length-1];
			}
		}
		Log.i(Constant.DIANBA,"file name="+xmlFile);
		List<Singer> result = new ArrayList<Singer>();
		Top10SingerService saxSingerService=new Top10SingerService();
		InputStream istream = null;
		if(exists){
			try {
				istream = new FileInputStream(xmlFile);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}else{
			Log.i(Constant.DIANBA,"not found ,read from self app.");
			istream=saxSingerService.getClass().getResourceAsStream(xmlFile);
		}
		
		
		try {
			result = saxSingerService.getService(istream);
			this.init(result, 0);
		} catch (Throwable e) {
			Log.i(Constant.DIANBA,">>>>>>>>>>>>>>get "+xmlFile+" error!["+e.getMessage()+"]");
		}
		return result;
	}
	
	
	// 要解析的XML文件以流的方式传进来
	private List<Singer> getService(InputStream istream) throws Throwable {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = saxParserFactory.newSAXParser();
		SingerHandler SingerHandler = new SingerHandler();
		saxParser.parse(istream, SingerHandler);
		// 当解析过程中遇到符合XML语法规定的语句时就会调用第2个参数所规定的方法
		return SingerHandler.getSingers();
	}
	
	
	private final class SingerHandler extends DefaultHandler {
		private List<Singer> singers;
		private Singer singer;
		private String TAG;// 存放当前所遍历的元素节点
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			super.characters(ch, start, length);
			if (TAG != null) {
				String data = new String(ch, start, length);
				if (singer != null) {
					if ("id".equals(TAG)) {
						singer.setId(data);
					} else if("name".equals(TAG)) {
						singer.setName(data);
					}else if ("desc".equals(TAG)) {
						singer.setDesc(data);
					} else if ("image_url".equals(TAG)) {
						singer.setImageUrl(data);
					} else if ("pinyin".equals(TAG)) {
						singer.setPinyin(data);
					} else if ("sex".equals(TAG)) {
						singer.setSex(data);
					} else if ("favorites_count".equals(TAG)) {
						singer.setFavoritesCount(Integer.parseInt(data));
					} else if ("area".equals(TAG)) {
						singer.setArea(data);
					} else if ("tags".equals(TAG)) {
						String[] ts = data.split(",");
						for(int i = 0;i<ts.length;i++){
							singer.addTag(new Tag(ts[i]));
						}
					} else if ("create_date".equals(TAG)) {
						singer.setCreateDate(data);
					} else if ("update_date".equals(TAG)) {
						singer.setUpdateDate(data);
					}
				}
			}
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
			singers = new ArrayList<Singer>();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			// localName:不含命名空间前缀的标签名
			// qName:含有命名空间前缀的标签名
			// attributes:接收属性值
			if ("singer".equals(localName)) {
				singer = new Singer();
			}
			
			TAG = localName;
		}

		public List<Singer> getSingers() {
			return singers;
		}		

		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			super.endElement(uri, localName, qName);
			if ("singer".equals(localName)) {
				singers.add(singer);
				singer = null;
			}
			TAG = null;
		}


		
	}
}
